import os
from fasttext import FTModel
from create_alarms_data_002 import ALL_ORIGIN_INFOS
import torch
from gragh_tools import draw
from torch_geometric.data.data import Data


def get_sentence_vectors(texts):
    word_embeddings_list = []
    for text in texts:
        encoded_input = FTModel.embedding_text_to_tensor(text)
        word_embeddings_list.append(encoded_input.unsqueeze(0))
    return torch.cat(word_embeddings_list, dim=0)


def translate_batch(batch):
    _x_list = [ALL_ORIGIN_INFOS[id] for id in batch.x.tolist()]
    batch.x = get_sentence_vectors(_x_list)
    _batch = batch.to("cuda:0")
    return _batch


"""
"10.88.130.10"
"/data1 磁盘空间剩余量过小",
"LIVE_PIC_PROXY,LIVE_UPGRADE_PROXY"
"10.10.112.44"
"http://10.10.112.44:9213/_cluster/health/?pretty 集群状态为yellow"
"LIVE_EPG_APP"
"""

MODEL_PATH = "/home/Dyf/code/storage_models/alarms/"
model = torch.load(os.path.join(MODEL_PATH, 'Alarms_{}.pth').format(2078))
model = model.to("cuda:0")
model.eval()
my_origin_data = {
    0: "LIVE_PIC_PROXY",
    1: "LIVE_UPGRADE_PROXY",
    2: "LIVE_EPG_APP",
    3: "10.88.130.10",
    4: "10.10.112.44",
}

batch = Data()
batch.x = torch.LongTensor([0, 1, 2, 3, 4])
_x_list = [my_origin_data[id] for id in batch.x.tolist()]
batch.x = get_sentence_vectors(_x_list)
# batch.edge_label_index=torch.LongTensor([[0,1,2,0,1,2,3,4,3,4],[3,3,3,4,4,4,5,6,5,6]]).to("cuda")
"""
0., 0., 1., 1., 1., 1., 0., 0., 1., 1.
"""
batch.edge_label_index = torch.LongTensor([[0,1,2,0,1,2], [3,3,3,4,4,4]]).to("cuda")
batch.edge_index = torch.LongTensor([[0,1,2,0,1,2], [3,3,3,4,4,4]]).to("cuda")
"""
tensor([5.7813e-04, 1.6781e-04, 5.7813e-04, 1.6781e-04, 9.9987e-01, 9.9790e-01,
        9.9983e-01, 9.9987e-01, 9.9794e-01, 9.9983e-01], device='cuda:0',
       grad_fn=<SigmoidBackward0>)
"""
batch.node_type = torch.LongTensor([[0, 0, 0, 1, 1]]).to("cuda")
_batch = batch.to("cuda:0")

# "10.88.130.10"
# "10.88.130.44"
# new_test_data.edge_index = torch.LongTensor([]).to("cuda")
# new_test_data.edge_type = torch.LongTensor([[], []]).to("cuda")
# new_test_data.edge_label = torch.LongTensor([]).to("cuda")

print(_batch.contains_isolated_nodes())
print(_batch.contains_self_loops())
print(_batch.is_directed())
out = model(_batch)
# print(new_test_data)
# print(new_test_data.node_id)
print(torch.round(out))
# print(torch.round(out).shape)
# print(new_test_data.edge_label_index)
# print(new_test_data.edge_label.shape)
# true_len = len([i for i in torch.round(out) == new_test_data.edge_label if i == True])
# all_len = len([i for i in torch.round(out) == new_test_data.edge_label])
# print(true_len,all_len)
